package com.hdoit.apollo.logical.inventory.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.apollo.logical.common.enums.inventory.EndType;
import com.hdoit.apollo.logical.inventory.dao.IInventoryDao;
import com.hdoit.apollo.logical.inventory.model.Adjustment;
import com.hdoit.apollo.logical.inventory.model.Inventory;
import com.hdoit.apollo.logical.inventory.model.InventoryCollocation;
import com.hdoit.apollo.logical.inventory.model.InventoryShow;
import com.hdoit.apollo.logical.inventory.service.IInventoryCollocationService;
import com.hdoit.apollo.logical.inventory.service.IInventoryService;
import com.hdoit.apollo.logical.inventory.service.IInventoryShowService;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.model.StockCollocation;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.stock.InventoryCollocationInfo;
import com.hdoit.apollo.logical.viewmodel.stock.InventorySearch;
import com.hdoit.apollo.logical.viewmodel.stock.InventoryShowSearch;
import com.tennetcn.core.common.enums.pub.YesOrNo;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月02日 14:46:14
 * @comment
 */
@Component
public class InventoryServiceImpl extends SuperService<Inventory> implements IInventoryService{
	@Autowired
	private IInventoryShowService inventoryShowService;
	@Autowired
	private IInventoryCollocationService inventoryCollocationService;
	@Autowired
	private IStockService stockService;
	@Autowired
	private IStockCollocationService stockCollocationService;
	@Autowired
	private IInventoryDao inventoryDao;
	@Override
	public boolean applyInventory(Inventory inventory, List<InventoryShow> inventoryShows) {
		for (InventoryShow inventoryShow : inventoryShows) {
			Stock stock=stockService.queryModelByKey(inventoryShow.getCargoId());
			if( stock!= null){
				stock.setModelStatus(ModelStatus.update);
				
				stock.setStockNum(inventoryShow.getActualNumShow());
				stockService.applyChange(stock);
			}
			List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
			for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
				
				for(StockCollocation stockCollocation:stock.getStockCollocations()) {
					if(stockCollocation.getCollocationId().equals(inventoryCollocation.getCollocationId())) {
						stockCollocation.setModelStatus(ModelStatus.update);
						stockCollocation.setCollocationNum(inventoryCollocation.getActualNumCollocation());
						stockCollocations.add(stockCollocation);
					}
				}
			}
			stockCollocationService.applyChanges(stockCollocations);
		}
		return super.applyChange(inventory);

	}
	@Override
	public boolean applyInventoryShow(Inventory inventory, List<InventoryShow> inventoryShows) {
		List<InventoryCollocation> inventoryCollocations = new ArrayList<InventoryCollocation>();
		if(super.applyChange(inventory)){
			if(inventory.getModelStatus() == ModelStatus.add){
				for (InventoryShow inventoryShow : inventoryShows) {
					if (StringUtils.isEmpty(inventoryShow.getShowId())) {
						inventoryShow.setShowId(CommonUtils.getKeyId());
					}
					inventoryShow.setInventoryId(inventory.getInventoryId());
					inventoryShow.setModelStatus(ModelStatus.add);
					
					Stock stock=stockService.queryModelByKey(inventoryShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						
						stock.setStockNum(inventoryShow.getActualNumShow());
						stockService.applyChange(stock);
					}
					List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
					for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
						if (StringUtils.isEmpty(inventoryCollocation.getInventoryCollocationId())) {
							inventoryCollocation.setInventoryCollocationId(CommonUtils.getKeyId());
						}
						inventoryCollocation.setModelStatus(ModelStatus.add);
						inventoryCollocations.add(inventoryCollocation);
						
						for(StockCollocation stockCollocation:stock.getStockCollocations()) {
							if(stockCollocation.getCollocationId().equals(inventoryCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(inventoryCollocation.getActualNumCollocation());
								stockCollocations.add(stockCollocation);
							}
						}
					}
					stockCollocationService.applyChanges(stockCollocations);
				}

			}else{
				for (InventoryShow inventoryShow : inventoryShows) {
					inventoryShow.setModelStatus(ModelStatus.update);
					for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
						inventoryCollocation.setModelStatus(ModelStatus.update);
						inventoryCollocations.add(inventoryCollocation);
					}
				}

			}

			return inventoryShowService.applyChanges(inventoryShows)&&inventoryCollocationService.applyChanges(inventoryCollocations);
		}
		return false;
	}
	@Override
	public Inventory queryModelById(String key) {
		return inventoryDao.queryModelById(key);
	}
	@Override
	public Adjustment queryAdjustmentById(String key) {
		return inventoryDao.queryAdjustmentById(key);
	}

	@Override
	public boolean saveNewInventory(Inventory inventory, String inventoryCargoIds) {
		List<InventoryShow> inventoryShows= new ArrayList<InventoryShow>();
	    List<InventoryCollocation> inventoryCollocations= new ArrayList<InventoryCollocation>();
	    
		InventoryShow inventoryShow = null;
		String[] cargoIds=inventoryCargoIds.split(",");
		
		for(int i=0,l=cargoIds.length;i<l;i++){
			inventoryShow=new InventoryShow();
			inventoryShow.setShowId(CommonUtils.getKeyId());
			inventoryShow.setInventoryId(inventory.getInventoryId());
			inventoryShow.setBookNumShow(isNull(inventoryShow.getBookNumShow()));
			Stock stock = stockService.queryModelByKey(cargoIds[i]);
			if(stock != null){
				inventoryShow.setBookNumShow(stock.getStockNum());
				for(StockCollocation stockCollocation:stock.getStockCollocations()) {
					InventoryCollocation inventoryCollocation=new InventoryCollocation();
					inventoryCollocation.setInventoryCollocationId(CommonUtils.getKeyId());
					inventoryCollocation.setCollocationId(stockCollocation.getCollocationId());
					inventoryCollocation.setShowId(inventoryShow.getShowId());
					inventoryCollocation.setOverageNumCollocation(0.0);
					inventoryCollocation.setLossNumCollocation(0.0);
					inventoryCollocation.setActualNumCollocation(0.0);
					inventoryCollocation.setBookNumCollocation(stockCollocation.getCollocationNum());
					inventoryShow.setBookNumShow(inventoryShow.getBookNumShow()+inventoryCollocation.getBookNumCollocation());
					inventoryCollocation.setModelStatus(ModelStatus.add);
					inventoryCollocations.add(inventoryCollocation);
				}
			}

			inventoryShow.setShopId(inventory.getShopId());
			inventoryShow.setCargoId(cargoIds[i]);
			inventoryShow.setDeleteMark(Integer.parseInt(YesOrNo.no));
			inventoryShow.setModelStatus(ModelStatus.add);
			inventoryShows.add(inventoryShow);
			inventory.setBookNum(isNull(inventory.getBookNum()));
			inventory.setBookNum(inventory.getBookNum()+inventoryShow.getBookNumShow());
			
		}
		if(super.applyChange(inventory)){
			return inventoryShowService.applyChanges(inventoryShows)&&inventoryCollocationService.applyChanges(inventoryCollocations);
		}
		return false;
	}
	@Override
	public boolean saveDraftInventoryInfo(Inventory inventory, InventoryCollocationInfo inventoryCollocationInfo) {
		
		String[] collocationIds=inventoryCollocationInfo.getCollocationId().split(",");
		//String[] cargoIds=inventoryCollocationInfo.getCargoId().split(",");
		String[] actualNumCollocations=inventoryCollocationInfo.getActualNumCollocation().split(",");
		
		List<InventoryShow> inventoryShows=new ArrayList<InventoryShow>();
        InventoryShowSearch inventoryShowSearch=new InventoryShowSearch();
        inventoryShowSearch.setInventoryId(inventory.getInventoryId());
        inventoryShows=inventoryShowService.queryListMP(inventoryShowSearch,null);
        List<InventoryCollocation> inventoryCollocations= new ArrayList<InventoryCollocation>();
        inventory.setOverageNum(0.0);
		inventory.setOverageTypeNum(0);
		inventory.setLossNum(0.0);
		inventory.setLossTypeNum(0);
		inventory.setActualNum(0.0);
	    inventory.setBookNum(0.0);
	    inventory.setActuaTypelNum(0);
		inventory.setBookTypeNum(0);
		
		for(InventoryShow inventoryShow:inventoryShows) {
	        inventoryShow.setOverageNumShow(0.0);
	        inventoryShow.setLossNumShow(0.0);
	        inventoryShow.setActualNumShow(0.0);
	        inventoryShow.setBookNumShow(0.0);
	        
			inventoryShow.setModelStatus(ModelStatus.update);
			for(InventoryCollocation inventoryCollocation:inventoryShow.getInventoryCollocations()) {
				inventoryCollocation.setModelStatus(ModelStatus.update);
				for(int i=0;i<collocationIds.length;i++){
					if(collocationIds[i].equals(inventoryCollocation.getCollocationId())){
						Double actualNumCollocation=Double.parseDouble(actualNumCollocations[i]);
						inventoryCollocation.setActualNumCollocation(actualNumCollocation);
						Double stockShow=actualNumCollocation-inventoryCollocation.getBookNumCollocation();
						
						inventoryShow.setActualNumShow(inventoryShow.getActualNumShow()+actualNumCollocation);
						inventoryShow.setBookNumShow(inventoryShow.getBookNumShow()+inventoryCollocation.getBookNumCollocation());
						if(stockShow>=0){
					        inventoryCollocation.setOverageNumCollocation(stockShow);
					        inventoryShow.setOverageNumShow(inventoryShow.getOverageNumShow()+stockShow);
				        }
						if(stockShow<0){
							inventoryCollocation.setLossNumCollocation(stockShow);
					        inventoryShow.setLossNumShow(inventoryShow.getLossNumShow()+stockShow);
					    }
					}
				}
				inventoryCollocations.add(inventoryCollocation);
			}
			if(inventoryShow.getOverageNumShow()>0){
		        inventory.setOverageNum(inventory.getOverageNum()+inventoryShow.getOverageNumShow());
		        inventory.setOverageTypeNum(inventory.getOverageTypeNum()+1);;
		      }
		      if(inventoryShow.getLossNumShow()<0){
		        inventory.setLossNum(inventory.getLossNum()+inventoryShow.getLossNumShow());
		        inventory.setLossTypeNum(inventory.getLossTypeNum()+1);
		      }
		      inventory.setActualNum(inventory.getActualNum()+inventoryShow.getActualNumShow());
		      inventory.setBookNum(inventory.getBookNum()+inventoryShow.getBookNumShow());
		}
		inventory.setActuaTypelNum(inventoryShows.size());
		inventory.setBookTypeNum(inventoryShows.size());
		
		if(EndType.adjustment.equals(inventory.getEndType())){
			for (InventoryShow inventoryShow : inventoryShows) {
				Stock stock=stockService.queryModelByKey(inventoryShow.getCargoId());
				if( stock!= null){
					stock.setModelStatus(ModelStatus.update);
					stock.setStockNum(inventoryShow.getActualNumShow());
					stockService.applyChange(stock);
				}
				List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
				for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
					for(StockCollocation stockCollocation:stock.getStockCollocations()) {
						if(stockCollocation.getCollocationId().equals(inventoryCollocation.getCollocationId())) {
							stockCollocation.setModelStatus(ModelStatus.update);
							stockCollocation.setCollocationNum(inventoryCollocation.getActualNumCollocation());
							stockCollocations.add(stockCollocation);
						}
					}
				}
				stockCollocationService.applyChanges(stockCollocations);
			}
		}
		
		if(super.applyChange(inventory)){
			return inventoryShowService.applyChanges(inventoryShows)&&inventoryCollocationService.applyChanges(inventoryCollocations);
		}
		return false;
	}
	private Double isNull(Double num) {
		if(num ==null) {
			return 0.0;
		}
		return num;
	}
	@Override
	public List<Inventory> queryListList(InventorySearch search, PagerModel pagerModel) {
		return inventoryDao.queryListList(search,pagerModel);
	}
	@Override
	public int queryCountList(InventorySearch search) {
		return inventoryDao.queryCountList(search);
	}

}
